package de.openlaboratory.util;

import java.io.File;
import java.io.Serializable;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Isolated Storage serves as a Gateway mechanism to hide the
 * file system details of a local file from web applications.
 * 
 * @author Yogi
 *
 */
public class IsolatedStorage implements Serializable
{
	private Path localFile;
	private String root;
	private String repository;
	private String fileName;

	/**
	 * 
	 */
	private static final long serialVersionUID = 3199654951942861644L;

	public IsolatedStorage(String root, String repository, String fileName)
	{
		this.root = root;
		this.repository = repository;
		this.fileName = fileName;
		
		localFile = Paths.get(constructPath());
	}

	private String constructPath()
	{
		String delim;
		
		StringBuilder s = new StringBuilder();

		LogManager.logMessage("ROOT: " + root);
		LogManager.logMessage("REPO: " + repository);
		LogManager.logMessage("FILE: " + fileName);
		
		String[] pathElements = new String[] {root, repository, fileName};
		
		for (String pathElmnt : pathElements)
		{			
			if (pathElmnt.contains("\\"))
				delim = "\\\\";
			else
				delim = "/";

			String[] subs = pathElmnt.split(delim);
			
			for (String t : subs)
			{
				if ((t != null) && (t.length() > 0))
					s.append(t).append(File.separator);
			}
		}
		
		String path = s.toString();
		path = path.substring(0, path.length() - 1);
		
		LogManager.logMessage("File path: " + path);
		
		return path; 
	}

	public Path getLocalFile()
	{
		return localFile;
	}

	public void setLocalFile(Path localFile)
	{
		this.localFile = localFile;
	}

	public Object getFileName()
	{
		return this.fileName;
	}

}
